/*
 *
 * author: fernado
 * create date: 2011-5-7
 */

package iceworld.fernado.file.impl;

import iceworld.fernado.consts.Constants;
import iceworld.fernado.entity.Field;
import iceworld.fernado.util.SystemResource;
import iceworld.fernado.util.Utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class SaveResource extends AbstractSaveResource {
	
	private static final String ID = SaveResource.class.getName();
	private static final Logger log = Logger.getLogger(ID);
	
	private static final String folder = SystemResource.getInstance().getProjectFolder() + "generator/";

	@Override
	public void removeResources() {
		File file = new File(folder);
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			if (null != files) {
				for (File f: files) {
					f.delete();
				}
			}
		}
	}

	@Override
	public void saveResources(String tableName, List<Field> fields) {
		generateLanguageFile(tableName, fields);		
	}
	
	protected void generateLanguageFile(String tableName, List<Field> fields) {
		String newTableName = generateTableName(tableName);
		newTableName = formatClazzName(newTableName);
		String comment = generateTableComment(tableName);
		String filename = folder + newTableName;
		String content = Constants.EMPTY_STRING;
		content = generateLanuageFrame(newTableName, comment, fields);
		log.log(Level.INFO, "THE CONTENT IS \r\n" + content);
		writeFile(filename, content);
	}
	
	protected String generateTableComment(String filename) {
		int j = 0;
		char[] chars = filename.toCharArray();
		for (int i = chars.length - 1; i > 0; i--) {
			j++;
			if (chars[i] > 128) {
				break;
			}
		}
		String result = filename.substring(0, chars.length - j + 1);
		result = result.substring(result.indexOf(")") + 1);
		
		// Chinese Space
		while (result.indexOf(" ") > -1) {
			result = result.replace(" ", "");
		}
		
		char[] chars2 = result.toCharArray();
		int k = 0;
		for (int i = 0; i < chars2.length; i++) {
			k++;
			if (chars2[i] > 128) {
				break;
			}
		}
		result = result.substring(k + 1);
		return result;
	}
	
	protected void generateContent(StringBuilder builder, Field field) {
		String type = field.getType();
		String name = field.getName();
//		String defaultValue = field.getDefaultValue();
		String comment = field.getComment();
		
		if (!Utils.isEmpty(type)) {
			if (!Utils.isEmpty(comment)) {
				addComment(builder, field);
			}
			
			builder.append("        ");
			builder.append("public");
			builder.append(" ");
			builder.append(type);
			builder.append(" ");
			builder.append(name);
			
			builder.append(" { ");
			builder.append("get; set;");
			builder.append(" } ");
			builder.append("\r\n");
			
//			if (!Utils.isEmpty(defaultValue)) {
//				builder.append(" = ");
//				builder.append(defaultValue);
//			}
//			builder.append(";");
		}
	}
	
	protected void addComment(StringBuilder builder, Field field) {
		builder.append("        ");
		builder.append("/** ");
		builder.append("\r\n");
		builder.append("         * ");
		builder.append(field.getComment());
		builder.append("\r\n");
		builder.append("         * ");
		builder.append(field.getZhName());
		builder.append("\r\n");
		builder.append("         * ");
		builder.append("none = ");
		builder.append(field.isNone());
		builder.append("\r\n");
		builder.append("        ");
		builder.append(" */");	
		builder.append("\r\n");
	}
	
	protected void addTableComment(StringBuilder builder, String tableComment) {
		builder.append("    ");
		builder.append("/**");
		builder.append("\r\n");
		builder.append("      * ");
		builder.append(tableComment);
		builder.append("\r\n");
		builder.append("    ");
		builder.append(" */");
		builder.append("\r\n");
	}
	
	private void writeFile(String filename, String content) {
		FileWriter fw = null;
		try {
			fw = new FileWriter(new File(filename + getLanguageSuffix()), true);
			fw.write(content);
		} catch (IOException e) {
			log.log(Level.INFO, "CANT OPEN FILE - " + filename + getLanguageSuffix());
		} finally {
			if (null != fw) {
				try {
					fw.close();
				} catch (IOException e) {
					log.log(Level.INFO, "CANT CLOSE FILE - " + filename + getLanguageSuffix());
				}
			}
		}
	}
	
	public abstract String getLanguageSuffix();
	public abstract String generateLanuageFrame(String clazzName, String tableComment, List<Field> fields);
}
